package GUI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import CTRLayer.*;

public class Suppliers extends JFrame {

	private JPanel contentPane;
	private JTextField txtID;
	private JTextField txtName;
	private JTextField txtPhone;
	private JTextField txtEmail;
	private JTextField txtAddress;
	private JTextField txtCity;
	private JTextField txtZIP;
	private JTable tableSupps;
	private JButton btnAddNew;
	private JButton btnShowInformation;
	private JButton btnDelete;
	private JButton btnBackToMain;
	private AddSupplier addsupp;
	private Controller ctr;
	private JTextField txtCountry;
	
	/**
	 * Create the frame.
	 */
	public Suppliers() {
		
		ctr = new Controller();
		
		setTitle("Supppliers");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 455, 470);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblName = new JLabel("Name:");
		lblName.setBounds(10, 36, 70, 14);
		contentPane.add(lblName);
		
		JLabel lblEmail = new JLabel("Email:");
		lblEmail.setBounds(10, 86, 70, 14);
		contentPane.add(lblEmail);
		
		JLabel lblAddress = new JLabel("Address:");
		lblAddress.setBounds(262, 11, 70, 14);
		contentPane.add(lblAddress);
		
		JLabel lblCity = new JLabel("City:");
		lblCity.setBounds(262, 36, 70, 14);
		contentPane.add(lblCity);
		
		JLabel lblZipCode = new JLabel("ZIP code:");
		lblZipCode.setBounds(262, 61, 70, 14);
		contentPane.add(lblZipCode);
		
		JLabel lblPhone = new JLabel("Phone:");
		lblPhone.setBounds(10, 61, 70, 14);
		contentPane.add(lblPhone);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(10, 11, 70, 14);
		contentPane.add(lblId);
		
		txtID = new JTextField();
		txtID.setBounds(79, 8, 86, 20);
		contentPane.add(txtID);
		txtID.setColumns(10);
		
		txtName = new JTextField();
		txtName.setBounds(79, 33, 86, 20);
		contentPane.add(txtName);
		txtName.setColumns(10);
		
		txtPhone = new JTextField();
		txtPhone.setBounds(79, 58, 86, 20);
		contentPane.add(txtPhone);
		txtPhone.setColumns(10);
		
		txtEmail = new JTextField();
		txtEmail.setBounds(79, 83, 86, 20);
		contentPane.add(txtEmail);
		txtEmail.setColumns(10);
		
		txtAddress = new JTextField();
		txtAddress.setBounds(342, 8, 86, 20);
		contentPane.add(txtAddress);
		txtAddress.setColumns(10);
		
		txtCity = new JTextField();
		txtCity.setBounds(342, 33, 86, 20);
		contentPane.add(txtCity);
		txtCity.setColumns(10);
		
		txtZIP = new JTextField();
		txtZIP.setBounds(342, 58, 86, 20);
		contentPane.add(txtZIP);
		txtZIP.setColumns(10);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.setBounds(160, 109, 90, 25);
		contentPane.add(btnSearch);
		
		
		tableSupps = new JTable();
		tableSupps.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tableSupps.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"ID", "Name", "Phone", "E-mail", "Address", "City", "ZIP", "Country"
			}
		) {
			Class[] columnTypes = new Class[] {
				String.class, String.class, String.class, String.class, String.class, String.class, String.class, String.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
			boolean[] columnEditables = new boolean[] {
				false, false, false, false, false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		tableSupps.getColumnModel().getColumn(0).setResizable(false);
		tableSupps.getColumnModel().getColumn(1).setResizable(false);
		tableSupps.getColumnModel().getColumn(2).setResizable(false);
		tableSupps.getColumnModel().getColumn(3).setResizable(false);
		tableSupps.getColumnModel().getColumn(4).setResizable(false);
		tableSupps.getColumnModel().getColumn(5).setResizable(false);
		tableSupps.getColumnModel().getColumn(6).setResizable(false);
		tableSupps.getColumnModel().getColumn(7).setResizable(false);
		tableSupps.setVisible(true);
		
		JScrollPane scrollPane = new JScrollPane(tableSupps);
		scrollPane.setBounds(9, 136, 419, 227);
		contentPane.add(scrollPane);
		

		
		
		btnAddNew = new JButton("Add new");
		btnAddNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				addsupp=new AddSupplier();
				addsupp.setVisible(true);
			}
		});
		btnAddNew.setBounds(10, 374, 115, 23);
		contentPane.add(btnAddNew);
		
		btnShowInformation = new JButton("Show information");
		btnShowInformation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int SelRowIndex = tableSupps.getSelectedRow();
				if(SelRowIndex==-1)
				{
					JOptionPane.showMessageDialog(null,
					    "You have to chose one from the list",
					    "Warning",
					    JOptionPane.WARNING_MESSAGE);
				}
				else
				{
					String idSelected = (String) tableSupps.getValueAt(SelRowIndex, 0);
					JOptionPane.showMessageDialog(null,
							ctr.suppInfo(idSelected));
					/*data about customer selected*/
				}
			}
		});
		btnShowInformation.setBounds(160, 374, 115, 23);
		contentPane.add(btnShowInformation);
		
		btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int SelRowIndex = tableSupps.getSelectedRow();
				if(SelRowIndex==-1)
				{
					JOptionPane.showMessageDialog(null,
					    "You have to chose one from the list",
					    "Warning",
					    JOptionPane.WARNING_MESSAGE);
				}
				else
				{
					String idSelected = (String) tableSupps.getValueAt(SelRowIndex, 0);		
					if(ctr.deleteSupplier(idSelected))
					{
						JOptionPane.showMessageDialog(null,
								"Deleted!"); 
					}
				}
			}
		});
		btnDelete.setBounds(313, 374, 115, 23);
		contentPane.add(btnDelete);
		
		btnBackToMain = new JButton("Back to Main Menu");
		btnBackToMain.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MainMenu.getInstance().setVisible(true);
				dispose();
			}
		});
		btnBackToMain.setBounds(147, 408, 144, 23);
		contentPane.add(btnBackToMain);
		
		JLabel lblCountry = new JLabel("Country:");
		lblCountry.setBounds(262, 86, 70, 14);
		contentPane.add(lblCountry);
		
		txtCountry = new JTextField();
		txtCountry.setBounds(342, 83, 86, 20);
		contentPane.add(txtCountry);
		txtCountry.setColumns(10);
		
		
		
	
	}
}
